package View.Components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import libs.TUIO.TuioCursor;

public class TouchButton extends JButton implements TouchObjectInterface {
	private static final long serialVersionUID = -7702141031288424860L;
	private ActionListener touchAction;
	private boolean customIcons = false;
	private ImageIcon icNormal, icLeft;

	public TouchButton(String string) {
		super(string);
	}

	public TouchButton(String string, ImageIcon icInside, ImageIcon icPressed, ImageIcon icLeft){
		super();
		this.setIcon(icInside);
		this.setPressedIcon(icPressed);
		this.setRolloverIcon(icLeft);
		icNormal = icInside;
		this.icLeft = icLeft;
		customIcons = true;
	}
	
	public int getInteractionType() {
		return mouseClickOnce;
	}

	public void clickOnce() {
	    if(touchAction == null)
	        return;
	    ActionEvent ae = new ActionEvent(this, 0, this.getText());
		touchAction.actionPerformed(ae);
	}

	public void addActionListener(ActionListener al) {
		touchAction = al;
	}

	public void updateMouse(boolean inside, TuioCursor tcur) {
		ButtonModel bm = getModel();
		if (inside){
			bm.setPressed(true);
			if(customIcons)
				this.setIcon(icNormal);
		}
		else{
			bm.setPressed(false);
			if(customIcons)
				this.setIcon(icLeft);
		}
		bm.setArmed(true);
	}

	public void revertUpdateMouse() {
		ButtonModel bm = getModel();
		bm.setArmed(false);
		bm.setPressed(false);
		bm.setEnabled(true);
		if(customIcons)
			this.setIcon(icNormal);
	}
}
